package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.mapper.InvoiceMapper;
import cn.wolfcode.shop.mapper.OrderInfoMapper;
import cn.wolfcode.shop.mapper.OrderProductMapper;
import cn.wolfcode.shop.mapper.OrderProductPropertyMapper;
import cn.wolfcode.shop.service.*;
import cn.wolfcode.shop.util.IdGenerateUtil;
import cn.wolfcode.shop.util.RedisConstants;
import cn.wolfcode.shop.vo.CarVo;
import cn.wolfcode.shop.vo.OrderVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.security.PrivateKey;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OderInfoServiceImpl implements IOrderInfoService {


    @Reference
    private IUserAddressService userAddressService;

    @Reference
    private IProductService productService;
    @Reference
    private IProductSkuService productSkuService;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private OrderProductPropertyMapper orderProductPropertyMapper;
    @Autowired
    private InvoiceMapper invoiceMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void createOrder(OrderVo orderVo) {
        UserLogin userLogin = orderVo.getUserLogin();
        OrderInfo orderInfo= new OrderInfo();
        orderInfo.setUserId(userLogin.getId());
        IdGenerateUtil idGenerateUtil = IdGenerateUtil.get();
        String orderSn = idGenerateUtil.nextId()+"";
        orderInfo.setOrderSn(orderSn);
        UserAddress userAddress = userAddressService.getById(orderVo.getUserAddressId());
        orderInfo.setAddress(userAddress.getCity());
        orderInfo.setAddress(userAddress.getConsignee());
        orderInfo.setAddress(userAddress.getCountry());
        orderInfo.setAddress(userAddress.getDistrict());
        orderInfo.setAddress(userAddress.getProvince());
        orderInfo.setAddress(userAddress.getPhone());
        orderInfo.setAddress(userAddress.getZipcode());
        orderInfo.setAddress(userAddress.getAddress());

        orderInfo.setOrderStatus(new Byte("0"));
        orderInfo.setFlowStatus(new Byte("0"));
        orderInfo.setPayStatus(new Byte("0"));
        orderInfo.setOrderTime(new Date());
        orderInfo.setPayType(orderVo.getPay().getPayType());

        BigDecimal amount=BigDecimal.ZERO;
        orderInfo.setOrderAmount(amount);
        orderInfoMapper.insert(orderInfo);
        for (CarVo car: orderVo.getCarVoList()) {
            OrderProduct orderProduct = new OrderProduct();
            orderProduct.setOrderId(orderInfo.getId());
            orderProduct.setSkuId(car.getSkuId());
            orderProduct.setProductNumber(car.getNumber());
            ProductSku productSku = productSkuService.getById(car.getSkuId());

            orderProduct.setProductPrice(productSku.getPrice());
            orderProduct.setTotalPrice(orderProduct.getProductPrice().multiply(new BigDecimal(orderProduct.getProductNumber())));

            amount=amount.add(orderProduct.getTotalPrice());

            Product product=productService.getProductId(productSku.getProductId());

            orderProduct.setProductName(product.getName());
            orderProduct.setProductImg(product.getImage());
            orderProductMapper.insert(orderProduct);

            productSku.getProductSkuPropertyList().forEach(productSkuProperty -> {
                OrderProductProperty orderProductProperty=new OrderProductProperty();
                orderProductProperty.setId(0L);
                orderProductProperty.setOrderProductId(0L);
                orderProductProperty.setName("");
                orderProductProperty.setValue("");
                orderProductPropertyMapper.insert(orderProductProperty);

            });
            String carKey=RedisConstants.USER_CAR
                    .replace("USERID",userLogin.getId()+"")
                    .replace("PRODUCTID",product.getId()+"")
                    .replace("SKUID",productSku.getId()+"");

            redisTemplate.delete(carKey);
        }
        orderInfo.setOrderAmount(amount);
        orderInfoMapper.updateByPrimaryKey(orderInfo);
        Invoice invoice=orderVo.getInvoice();
        if(invoice!=null){
            invoice.setUserId(userLogin.getId());
            invoice.setOrderId(orderInfo.getId());
            invoiceMapper.insert(invoice);
        }

    }
}
